package com.colin.jipay.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.colin.jipay.BaseActivity;
import com.colin.jipay.R;
import com.colin.jipay.WebService;
import com.colin.jipay.utils.AbSharedUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.colin.jipay.activity.MainActivity.sendmsg;
import static com.colin.jipay.config.Configs.LOGIN_URL;

public class LoginActivity extends BaseActivity {


    @BindView(R.id.account)
    EditText accountEdt;
    @BindView(R.id.password)
    EditText passwordEdt;
    @BindView(R.id.login)
    Button loginBtn;

    private String account, password;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        if (!TextUtils.isEmpty(AbSharedUtil.getString(this, "account")) && !TextUtils.isEmpty(AbSharedUtil.getString(this, "password"))) {
            Login(AbSharedUtil.getString(this, "account"), AbSharedUtil.getString(this, "password"));
        }
    }

    @Override
    public void onBackPressed() {

    }

    private void Login(final String account, final String password) {
        HttpUtils httpUtils = new HttpUtils(30000);
        RequestParams requestParams = new RequestParams();
        requestParams.addBodyParameter("user", account);
        requestParams.addBodyParameter("pass", password);
        loadingProgress.show();
        httpUtils.send(HttpRequest.HttpMethod.POST, LOGIN_URL, requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                Log.d("--", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getString("code").equals("1")) {

                        String sign = jsonObject.getString("sign");
                        String url = jsonObject.getString("url");
                        String title = jsonObject.getString("title");

                        AbSharedUtil.putString(getApplicationContext(), "sign", sign);
                        AbSharedUtil.putString(getApplicationContext(), "url", url);
                        AbSharedUtil.putString(getApplicationContext(), "ip", url);
                        AbSharedUtil.putString(getApplicationContext(), "account", account);
                        AbSharedUtil.putString(getApplicationContext(), "password", password);

//                        sendmsg("账号："+ AbSharedUtil.getString(getApplicationContext(), "account")+" 登录成功!" );
//                        sendmsg("启动WS服务!");
//                        //webcoket开始处理
//                        String w = url;
//                        w = w.replace("https", "ws");
//                        w = w.replace("http", "ws");
//                        w = w+":9501";
//                        sendmsg( "正在连接WS"  );
//                        AbSharedUtil.putString(getApplicationContext(), "wscode", "ok");
//                        AbSharedUtil.putString(getApplicationContext(), "ws", w);

                        loadingProgress.cancel();
                        ToastUtils.showShort("登录成功");
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        LoginActivity.this.startActivity(intent);
                        finish();
                    } else {
                        loadingProgress.cancel();
                        ToastUtils.showShort(jsonObject.getString("msg"));
                    }
                } catch (JSONException e) {
                    loadingProgress.cancel();
                    ToastUtils.showShort("登录失败：" + e.getMessage());
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingProgress.cancel();
                ToastUtils.showShort("登录失败：" + e.getMessage() + "\n" + s);
                e.printStackTrace();
            }
        });
    }

    @OnClick(R.id.login)
    public void onViewClicked() {
        account = accountEdt.getText().toString();
        password = passwordEdt.getText().toString();
        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) {
            Login(account, password);
        } else {
            Toast.makeText(LoginActivity.this, "请输入账号密码!", Toast.LENGTH_LONG).show();
        }
    }
}
